package cn.iocoder.yudao.module.member.controller.admin.user;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.CheckUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.member.controller.admin.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.CrmMemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserGasAddressDO;
import cn.iocoder.yudao.module.member.service.user.CrmMemberUserService;
import cn.iocoder.yudao.module.trade.api.order.dto.DepositOrderDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderPageDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderPageItemRespDTO;
import cn.iocoder.yudao.module.trade.api.order.dto.TradeOrderRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.*;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 燃气户")
@RestController
@RequestMapping("/crm/member/user")
@Validated
public class CrmMemberUserController {
    @Resource
    private CrmMemberUserService crmMemberUserService;

    @PostMapping("/create")
    @Operation(summary = "创建燃气户")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('crm:member-user:create')")
    public CommonResult<Long> createMemberUser(@Valid @RequestBody CrmMemberUserCreateReqVO createReqVO, @RequestHeader Integer terminal) {
        createReqVO.setTerminal(terminal);
        return success(crmMemberUserService.createMemberUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新燃气户")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('crm:member-user:update')")
    public CommonResult<Boolean> updateMemberUser(@Valid @RequestBody CrmMemberUserUpdateReqVO updateReqVO) {
        crmMemberUserService.updateMemberUser(updateReqVO);
        return success(true);
    }

    @PutMapping("/update/phone")
    @Operation(summary = "更新联系人")
    @PermitAll
    public  CommonResult<List<MemberUserRespVO>> updateMemberPhone(@RequestBody CrmMemberUserUpdatePhoneReqVO crmMemberUserUpdatePhoneReqVO){
        if(CheckUtils.isEmpty(crmMemberUserUpdatePhoneReqVO.getId())){
            return success(null);
        }
        return crmMemberUserService.updateMemberPhone(crmMemberUserUpdatePhoneReqVO);
    }



    @DeleteMapping("/delete")
    @Operation(summary = "删除燃气户")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true)
    //@PreAuthorize("@ss.hasPermission('crm:member-user:delete')")
    public CommonResult<Boolean> deleteMemberUser(@RequestParam("id") Long id) {
        crmMemberUserService.deleteMemberUser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得燃气户")
    @PermitAll
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<CrmMemberUserRespVO> getMemberUser(@RequestParam("id") Long id) {
        MemberUserGasAddressDO memberUser = crmMemberUserService.getMemberUser(id);
        return success(CrmMemberUserConvert.INSTANCE.convert(memberUser));
    }

    @GetMapping("/get/code")
    @Operation(summary = "自动生成code")
    @PermitAll
    @Parameter(name = "key", description = "key表名或者业务名", required = true, example = "crm_id")
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<String> getCode(@RequestParam("key") String key) {
        //// 新生成规则
        String code = crmMemberUserService.getCodeByDate(key);
        return success(code);
    }

    @GetMapping("/get/dates")
    @Operation(summary = "获得日期数组")
    @PermitAll
    @Parameter(name = "beginTime", description = "开始时间", required = true)
    @Parameter(name = "endTime", description = "结束时间", required = true)
    //@PreAuthorize("@ss.hasPermission('member:user:query')")
    public CommonResult<List<String>> getDates(@RequestParam("beginTime") String beginTime,@RequestParam("endTime") String endTime) {
        List<String> dates = crmMemberUserService.getDates(beginTime,endTime);
        return success(dates);
    }

    @GetMapping("/list")
    @Operation(summary = "获得燃气户列表")
    @PermitAll
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<List<CrmMemberUserRespVO>> getMemberUserList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberUserGasAddressDO> list = crmMemberUserService.getMemberUserList(ids);
        return success(CrmMemberUserConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得燃气户分页")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<PageResult<CrmMemberUserRespVO>> getMemberUserPage(@Valid CrmMemberUserPageReqVO pageVO) {
        PageResult<CrmMemberUserRespVO> pageResult = crmMemberUserService.getMemberUserPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出燃气户 Excel")
    @PreAuthorize("@ss.hasPermission('crm:member-user:export')")
    public void exportMemberUserExcel(@Valid CrmMemberUserExportReqVO exportReqVO,
                                      HttpServletResponse response) throws IOException {
        List<MemberUserGasAddressDO> list = crmMemberUserService.getMemberUserList(exportReqVO);
        // 导出 Excel
        List<CrmMemberUserExcelVO> datas = CrmMemberUserConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "燃气户.xls", "数据", CrmMemberUserExcelVO.class, datas);
    }

    @GetMapping("/get/order/page")
    @Operation(summary = "获得燃气户订单")
    @PermitAll
    //@PreAuthorize("@ss.hasPermission('crm:member-user:query')")
    public CommonResult<PageResult<TradeOrderPageItemRespDTO>> getOrderByCrmId(TradeOrderPageDTO dto) {
        PageResult<TradeOrderPageItemRespDTO> pageResult = crmMemberUserService.getOrderByCrmId(dto);
        return success(pageResult);
    }

    @PutMapping("/examine")
    @Operation(summary = "审核燃气户")
    @PermitAll
    @PreAuthorize("@ss.hasPermission('crm:member-user:update')")
    public CommonResult<Boolean> examineMemberUser(@Valid @RequestBody CrmMemberUserUpdateReqVO updateReqVO) {

        crmMemberUserService.examineMemberUser(updateReqVO);
        return success(true);
    }
}
